﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DataAccessLayer.DataInterfaces;
using MyCommon;

namespace DataAccessLayer
{
    public class NguoiDungDAO : INguoiDung
    {
        private static QLBaoHanh dataContext;
        private IRepository<NguoiDung> repository;
        public NguoiDungDAO()
        {
            dataContext = new QLBaoHanh();
            repository = new RepositoryDAO<NguoiDung>(dataContext);
        }

        public IEnumerable<NguoiDung> GetAllNguoiDung()
        {
            //return repository.GetAll();
            return repository.GetQuery().OrderBy(aobj => aobj.Id);
        }

        public IEnumerable<NguoiDung> GetAllNguoiDungChuaSuDung()
        {
            //ListIdNguoiDungDaSuDung
            var a = dataContext.NhanViens.Select(o => o.NguoiDungId).Distinct();
            return dataContext.NguoiDungs.Where(b => !a.Contains(b.Id)).Select(d => (NguoiDung)d).OrderBy(e => e.Username);            
        }

        public bool IsUsernameExist(string inUsername)
        {
            if (dataContext.NguoiDungs.Any(o => o.Username == inUsername))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool IsPwdMatch(string inUsername, string inPwd)
        {
            if (dataContext.NguoiDungs.Any(o => o.Username == inUsername && o.Password == inPwd))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public bool IsActiveAccount(string inUsername)
        {
            if (dataContext.NguoiDungs.Any(o => o.Username == inUsername && o.TrangThai == 1))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public NguoiDung GetNguoiDungByUsername(string inUsername)
        {
            return dataContext.NguoiDungs.Where(o => o.Username == inUsername).SingleOrDefault<NguoiDung>();
        }

        public NguoiDung GetNguoiDungById(int Id)
        {
            return repository.Find(obj => obj.Id == Id).Single();
        }
        public NguoiDung GetLastRecord()
        {
            return dataContext.NguoiDungs.OrderByDescending(obj => obj.Id).FirstOrDefault();
        }

        public bool UpdateNguoiDung(NguoiDung inNguoiDung)
        {
            if (inNguoiDung == null) return false;
            NguoiDung upNguoiDung = repository.First(sobj => sobj.Id == inNguoiDung.Id);
            if (upNguoiDung == null) return false;
            upNguoiDung = inNguoiDung;
            return repository.SaveChanges() > 0;
        }
        public bool InsertNguoiDung(NguoiDung inNguoiDung)
        {
            if (inNguoiDung == null) return false;
            repository.Add(inNguoiDung);
            return repository.SaveChanges() > 0;
        }

        public ReturnValue<bool> DelNguoiDung(int Id)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            try
            {
                NguoiDung objDel = repository.First(obj => obj.Id == Id);
                ret = DelNguoiDung(objDel);
            }
            catch (Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }


        public ReturnValue<bool> DelNguoiDung(NguoiDung inNguoiDung)
        {
            ReturnValue<bool> ret = new ReturnValue<bool>(false, "");
            if (inNguoiDung == null)
            {
                ret.Msg = "Input null";
                return ret;
            }
            try
            {
                repository.Delete(inNguoiDung);
                ret.RetValue = repository.SaveChanges() > 0;
            }
            catch (System.Exception ex)
            {
                ret.RetValue = false;
                ret.Msg = "Dữ liệu đã được sử dung trong chương trình - không thể xóa !";
            }
            return ret;
        }
        
        public IEnumerable<VaiTro> GetAllVaiTro()
        {
            return dataContext.VaiTroes.Select(o => (VaiTro)o);
        }
    }
}
